package graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Panel;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.Timer;

import physics.Ball;
import physics.Coordinate;
import physics.Vector;
import physics.World;
import physics.shape.Circle;



public class gameTest extends Panel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final int FPS = 30;
	Timer timer;
	Ball ball;
	Circle field;
	World world;
	 BufferedImage panel = new BufferedImage(600,600, BufferedImage.TYPE_INT_ARGB);
	 
	 public gameTest(){
		 field = new Circle(new Coordinate(200,100), 100);
		 ball = new Ball(50,210,2);
		 world = new World();
		 world.addObstacle(ball);
		 timer = new Timer(1000/FPS, new ActionListener(){

				
				
				
				//timer to set when to draw the screen and update values
				@Override
				public void actionPerformed(ActionEvent arg0) {
					draw();
					ball.updatePosition();
					timer.restart();
					int x = (int)ball.getLocation().x;
					int y = (int)ball.getLocation().y;
					Rectangle rec1 = new Rectangle(100, 200, 100,100);
					if (rec1.contains(x, y)){
						ball.getVector().addVector(new Vector(Coordinate.getAngle(ball.getLocation(), new Coordinate(rec1.getCenterX(),rec1.getCenterY())), 0.1f));
						
					}
					
					if (x>300 && x<400 && y<500 && y > 400 ){
						ball.getVector().addVector(new Vector(Coordinate.getAngle(ball.getLocation(), new Coordinate(350,450)), -0.09f));
					}
					
					if (x>400 && x<500 && y<500 && y > 400 ){
						ball.getVector().addVector(new Vector(Coordinate.getAngle(ball.getLocation(), new Coordinate(450,450)), 0.5f));
						System.out.println("angle: " +Coordinate.getAngle(ball.getLocation(), new Coordinate(450,450)));

					}
					
				}

				});
		 timer.start();
		 JFrame jf = new JFrame("test");
		  this.setFocusable(true);
		  this.requestFocusInWindow();
		   jf.setFocusable(true);
		   jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	  
		   jf.getContentPane().add(this);
		   jf.getContentPane().setPreferredSize(new Dimension(600,600));	 
		   jf.validate();
		   jf.pack();
		   jf.setVisible(true);
		   world.start();
	 }
	 @Override
		public void update(Graphics g){
		       paint(g);
		}
	 public void draw(){
		 panel = new BufferedImage(600,600, BufferedImage.TYPE_INT_ARGB);
		
		 Graphics2D g = (Graphics2D) panel.getGraphics();
		 g.setColor(Color.LIGHT_GRAY);
		 g.fillRect(0, 0, 600,600);
		 g.setColor(Color.blue);
		// g.drawOval((int)(ball.getLocation().x), (int)ball.getLocation().y,6, 6);
		 
		 WorldDrawer.drawWorld(g, world);
		 g.setColor(Color.GREEN);
		g.drawRect(100, 200, 100,100);
		g.drawRect(400, 400, 100,100);
		g.drawOval(150, 200+50, 2, 2);
		g.drawOval(450, 400+50, 2, 2);
		 g.setColor(Color.RED);
		g.drawRect(300, 400, 100,100);
		g.drawOval(350, 400+50, 2, 2);
		// g.drawOval((int)110, (int)110,6, 6);
		 g.dispose();
			repaint();
	 }
	 
	
		
		@Override
		public void paint(Graphics g) {
			if(panel == null){
				panel = new BufferedImage(600,600, BufferedImage.TYPE_INT_ARGB);
				draw();
				}
			g.drawImage(panel,0,0,getWidth(),getHeight(),null);
			
		}
		
		public static void main(String args[]){
			new gameTest();
		}
	
}
